﻿using System;

using LightningTools.BCSTesterMan.Common.Infrastructure;

using Microsoft.SharePoint.BusinessData.Administration.Client;

namespace LightningTools.BCSTesterMan.Model
{
    public class Parameter : ObservableObject
    {
        private string name;

        private Type type;
        private object value;
        private string filter;
        private string filterField;
        private string fieldAppliedTo;
        private bool isReadOnly;
        private bool isIdentifier;

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                RaisePropertyChanged("Name");
            }
        }

        /// <summary>
        /// Gets or sets the type.
        /// </summary>
        /// <value>The type.</value>
        public Type Type
        {
            get { return type; }
            set
            {
                type = value;
                RaisePropertyChanged("Type");
            }
        }

        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        /// <value>The value.</value>
        public object Value
        {
            get
            {
                if (value == null)
                {
                    return GetDefault(Type);
                }

                return value;
            }
            set
            {
                this.value = value;
                RaisePropertyChanged("Value");
            }
        }

        /// <summary>
        /// Gets or sets the filter.
        /// </summary>
        /// <value>The filter.</value>
        public string Filter
        {
            get
            {
                return filter;
            }
            set
            {
                filter = value;
                RaisePropertyChanged("Filter");
            }
        }

        /// <summary>
        /// Gets or sets the filter field.
        /// </summary>
        /// <value>The filter field.</value>
        public string FilterField
        {
            get
            {
                return filterField;
            }
            set
            {
                filterField = value;
                RaisePropertyChanged("FilterField");
            }
        }

        /// <summary>
        /// Gets or sets the field applied to.
        /// </summary>
        /// <value>The field applied to.</value>
        public string FieldAppliedTo
        {
            get {
                return fieldAppliedTo;
            }
            set {
                fieldAppliedTo = value;
                RaisePropertyChanged("FieldAppliedTo");
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is read only.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is read only; otherwise, <c>false</c>.
        /// </value>
        public bool IsReadOnly
        {
            get {
                return isReadOnly;
            }
            set {
                isReadOnly = value;
                RaisePropertyChanged("IsReadOnly");
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is identifier.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is identifier; otherwise, <c>false</c>.
        /// </value>
        public bool IsIdentifier
        {
            get {
                return isIdentifier;
            }
            set {
                isIdentifier = value;
                RaisePropertyChanged("IsIdentifier");
            }
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return Name;
        }

        /// <summary>
        /// Gets the default type for the Data Type.
        /// </summary>
        /// <param name="dataType">Type of the data.</param>
        /// <returns></returns>
        public static object GetDefault(Type dataType)
        {
            if (dataType.IsValueType)
            {
                return Activator.CreateInstance(dataType);
            }
            return null;
        }

    }
}